package com.zx.znav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.znav.bean.User;
import com.zx.znav.service.UserService;
import com.zx.znav.mapper.UserMapper;
import com.zx.znav.utils.MD5Utils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author lenovo
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2023-08-15 13:16:30
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{
    @Override
    public User checkLogin(String username, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        wrapper.eq(User::getPassword, password);
        return getOne(wrapper);
    }

    @Override
    public void saveUser(User user) {
        user.setPassword(MD5Utils.code(user.getPassword()));    //加密
        user.setAvatar("https://avatars.githubusercontent.com/u/81242825?v=4");
        user.setNickname(user.getUsername());
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setAdmin(false);
        save(user);
    }

    @Override
    public boolean checkRegister(String email) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        return getOne(wrapper) == null;
    }
}



